class Solution {
public:
    class comparision {
    public:
        bool operator()(const pair<int, int>& l1, const pair<int, int>& l2) {
            return l1.second > l2.second;
        }
    };
    vector<int> topKFrequent(vector<int>& nums, int k) {

      unordered_map<int,int> map;
      for(int i = 0 ;i < nums.size();i++){
        map[nums[i]]++;
      }

      priority_queue<pair<int,int>,vector<pair<int,int>>,comparision > pri_que;

      for(unordered_map<int,int>::iterator it = map.begin();it != map.end();it++){
        pri_que.push(*it);
        if(pri_que.size() > k){
          pri_que.pop();
        }
      }
    
      vector<int> res;
      for(int i = k;i > 0;i--){
        res.push_back(pri_que.top().first);
        pri_que.pop();
      }
      return res;
    }
};
